﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

using VedioOnline.Tools;
using VedioOnline.Tools.Dao;

namespace VedioOnline.Controllers
{
    public class DisplayController : Controller
    {
        BaseDao dao = new BaseDao();
        // GET: Display
        [GetIp]
        public ActionResult Index()
        {
            string sql = "";

            sql = "select top 6 * from v_TypeDY order by TypeDYId desc";
            var DYList = dao.GetList(sql, new string[] { "TypeDYId", "SubjectId", "Title", "ImageUrl" });

            sql = "select top 6 * from v_TypeJJ order by TypeJJId desc";
            var JJList = dao.GetList(sql, new string[] { "TypeJJId", "SubjectId", "Title", "ImageUrl" });

            sql = "select top 6 * from v_TypeZY order by TypeZYId desc";
            var ZYList = dao.GetList(sql, new string[] { "TypeZYId", "SubjectId", "Title", "ImageUrl" });

            ViewBag.DYList = DYList;
            ViewBag.JJList = JJList;
            ViewBag.ZYList = ZYList;

            return View("Index");
        }

        [GetIp]
        public ActionResult Type_DYView()
        {
            string Page = Request.QueryString["Page"] == null ? null : Request.QueryString["Page"].ToString();
            var rx = new Regex(@"^[0-9]*$");
            if (Page == null || !rx.IsMatch(Page))
            {
                return RedirectToAction("Index");
            }
            try
            {
                string sql = "";
                int PageNum = int.Parse(Page);

                sql = " select top 15 * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by TypeDYId desc) as rowNumber,TypeDYId,SubjectId,Title,ImageUrl" +
                        " FROM v_TypeDY " +
                        " where 1=1 " +
                        " ) as s " +
                        " where rowNumber>" + (15 * (PageNum - 1)).ToString();
                var SubjectList = dao.GetList(sql, new string[] { "TypeDYId", "SubjectId", "Title", "ImageUrl" });
                string SqlNum = "select count(*) as TotalNum FROM v_TypeDY where 1=1 ";
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });
                int Count = int.Parse(ListNum[0]["TotalNum"].ToString());
                ViewBag.SubjectList = SubjectList;
                ViewBag.Count = Count;
                ViewBag.PageNum = PageNum;


                return View("Type_DY");
            }
            catch (Exception ex)
            {

                return Content("发生错误了:" + ex.Message);
            }

        }

        [GetIp]
        public ActionResult Type_JJView()
        {
            string Page = Request.QueryString["Page"] == null ? null : Request.QueryString["Page"].ToString();
            var rx = new Regex(@"^[0-9]*$");
            if (Page == null || !rx.IsMatch(Page))
            {
                return RedirectToAction("Index");
            }
            try
            {
                string sql = "";
                int PageNum = int.Parse(Page);

                sql = " select top 15 * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by TypeJJId desc) as rowNumber,TypeJJId,SubjectId,Title,ImageUrl" +
                        " FROM v_TypeJJ " +
                        " where 1=1 " +
                        " ) as s " +
                        " where rowNumber>" + (15 * (PageNum - 1)).ToString();
                var SubjectList = dao.GetList(sql, new string[] { "TypeJJId", "SubjectId", "Title", "ImageUrl" });
                string SqlNum = "select count(*) as TotalNum FROM v_TypeJJ where 1=1 ";
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });
                int Count = int.Parse(ListNum[0]["TotalNum"].ToString());
                ViewBag.SubjectList = SubjectList;
                ViewBag.Count = Count;
                ViewBag.PageNum = PageNum;


                return View("Type_JJ");
            }
            catch (Exception ex)
            {

                return Content("发生错误了:" + ex.Message);
            }

        }

        [GetIp]
        public ActionResult Type_ZYView()
        {
            string Page = Request.QueryString["Page"] == null ? null : Request.QueryString["Page"].ToString();
            var rx = new Regex(@"^[0-9]*$");
            if (Page == null || !rx.IsMatch(Page))
            {
                return RedirectToAction("Index");
            }
            try
            {
                string sql = "";
                int PageNum = int.Parse(Page);

                sql = " select top 15 * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by TypeZYId desc) as rowNumber,TypeZYId,SubjectId,Title,ImageUrl" +
                        " FROM v_TypeZY " +
                        " where 1=1 " +
                        " ) as s " +
                        " where rowNumber>" + (15 * (PageNum - 1)).ToString();
                var SubjectList = dao.GetList(sql, new string[] { "TypeZYId", "SubjectId", "Title", "ImageUrl" });
                string SqlNum = "select count(*) as TotalNum FROM v_TypeZY where 1=1 ";
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });
                int Count = int.Parse(ListNum[0]["TotalNum"].ToString());
                ViewBag.SubjectList = SubjectList;
                ViewBag.Count = Count;
                ViewBag.PageNum = PageNum;


                return View("Type_ZY");
            }
            catch (Exception ex)
            {

                return Content("发生错误了:" + ex.Message);
            }


        }


        [GetIp]
        public ActionResult DetailView()
        {
            string SubjectId = Request.QueryString["SubjectId"] == null ? null : Request.QueryString["SubjectId"].ToString();

            var rx = new Regex(@"^[0-9]*$");
            if (SubjectId == null || !rx.IsMatch(SubjectId) )
            {
                return RedirectToAction("Index");
            }
            try
            {
                string sql = "select * from t_o_SubVid where SubjectId=" + SubjectId + " order by SortNum asc";
                var VideoList = dao.GetList(sql, new string[] { "VideoId" });
                ViewBag.VideoList = VideoList;

                sql = "select * from t_o_Subject where SubjectId=" + SubjectId;
                var SubjectMap = dao.GetList(sql, new string[] { "Content", "Title", "ImageUrl","SubjectId" })[0];
                ViewBag.SubjectMap = SubjectMap;
            }
            catch (Exception ex)
            {
                return Content("发生错误了:"+ex.Message);
            }            
            return View("Detail");
        }

        [GetIp]
        public ActionResult PlayView()
        {
            string VideoId = Request.QueryString["VideoId"] == null ? null : Request.QueryString["VideoId"].ToString();
            string SubjectId = Request.QueryString["SubjectId"] == null ? null : Request.QueryString["SubjectId"].ToString();
            var rx = new Regex(@"^[0-9]*$");
            if ( VideoId == null || !rx.IsMatch(VideoId) || !rx.IsMatch(SubjectId) )
            {
                
            }
            ViewBag.SubjectId = SubjectId;
            try
            {
                string sql = "";
                sql = "select * from t_o_SubVid where SubjectId=" + SubjectId + " order by SortNum asc";
                var VideoList = dao.GetList(sql, new string[] { "VideoId" });
                ViewBag.VideoList = VideoList;

                sql = "select * from t_d_UpLoadVideo where VideoId=" + VideoId;
                var VideoMap = dao.GetList(sql, new string[] { "m3u8Path", "VttUrl","VideoId" })[0];
                ViewBag.VideoMap = VideoMap;
                return View("Play");


            }
            catch (Exception ex)
            {
                return Content("发生错误了:" + ex.Message);
            }


            
        }

        [GetIp]
        public ActionResult search()
        {
            string wd = Request.Form["wd"] == null ? null : Request.Form["wd"].ToString();
            if(wd==null || wd == "请输入关键词")
            {
                return RedirectToAction("Index");
            }
            ViewBag.SearchText = wd;
            string sql = "select * from t_o_Subject where IsDelete<>1 and Title like '%" + wd + "%'";
            var SubjectList = dao.GetList(sql, new string[] {  "SubjectId", "Title", "ImageUrl" });
            ViewBag.SubjectList = SubjectList;
            return View("Search");


        }



    }
}